﻿using System;
using System.Data;
using LIB.Core;
using LIB.Data;
using System.ServiceModel;
using System.ServiceModel.Activation;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class wns
{
    [OperationContract]
    public string Fs_TUYEN_SINH_TTCHUNG()
    {
        try
        {
            DanhMuc dm = new DanhMuc();
            DataTable b_dt1 = c_ht.Fdt_DAN_TOC_LKE();
            DataTable b_dt2 = c_ht.Fdt_TON_GIAO_LKE();
            string b_pb = dm.Fs_PHONG_BAN_LKE();
            string cv = dm.Fs_CV_LKE();
            string lhd = dm.Fs_HD_LKE();
            string lcs = dm.Fs_CS_LKE();
            string tb = dm.Fs_TD_LKE();

            DataTable b_dt_dt = c_ht.Fdt_DT_LKE();

            return LibTable.FormTableToJson(b_dt1) + "@" + LibTable.FormTableToJson(b_dt2) + "@" + b_pb + "@" + cv + "@" + lhd + "@" + lcs + "@" + tb
                + "@" + LibTable.FormTableToJson(b_dt_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //

    [OperationContract]
    public string Fs_NHANSU_NH(object b_so_id, string b_url, string[] a_ten, string[] a_gtri, object[] a_qhgd, object[] a_gtriGridDT, object[] a_gtriGridCT,
            object[] a_gtriGridKT, object[] a_gtriGridKL)
    {
        try
        {
            DataTable b_dt = LibBang.Fdt_TAO_BANG(a_ten, a_gtri);
            if (b_dt.Rows.Count == 0) throw new Exception("Không lấy được dữ liệu");
            DataRow b_dr = b_dt.Rows[0];
            object[] b_gtri = new object[] { b_so_id,
            LibConvert.toString(b_dr["hodem"]),  LibConvert.toString(b_dr["Ten"]), b_url, LibConvert.toString(b_dr["bidanh"])
            , LibConvert.DateToNumber(b_dr["ngaysinh"]), LibConvert.toString(b_dr["gioitinh"]), LibConvert.toString(b_dr["so_hc"]), 
            LibConvert.toString(b_dr["cmt"]), LibConvert.DateToNumber(b_dr["ngay_cap"])
            , LibConvert.toString(b_dr["noi_cap"]), LibConvert.toString(b_dr["que_quan"]),
            LibConvert.toString(b_dr["dan_toc"]), LibConvert.toString(b_dr["noi_sinh"])       
            , LibConvert.toString(b_dr["ton_giao"]), LibConvert.toString(b_dr["noi_o"]),
            LibConvert.toString(b_dr["tinh_trang"]), LibConvert.toString(b_dr["phong_ban"])
            , LibConvert.toString(b_dr["chuc_vu"]), LibConvert.toString(b_dr["hoc_van"])
            , LibConvert.toString(b_dr["hop_dong"]), LibConvert.DateToNumber(b_dr["ngay_tuyen"])
            , LibConvert.toString(b_dr["so_dt"]), LibConvert.toString(b_dr["chinh_sach"])
            , LibConvert.DateToNumber(b_dr["ngay_nn"]), LibConvert.DateToNumber(b_dr["ngay_xn"])
            };
            object[] id_ns = SqlData.ExeCuteProcedure(b_gtri, "@id", "PNHAN_SU_NH");

            string b_so_id_ns = string.Empty;
            b_so_id_ns = LibConvert.toString(id_ns[0]);
            // Nhap thong tin gia dinh
            DataTable b_dt_qh = new DataTable();
            LibTable.AddNewCol(ref  b_dt_qh, new string[] { "QH_ID", "MA_SV", "loai", "hoten", "nam_sinh", "nghe_nghiep", "dan_toc", "ton_giao", "hktt" });
            for (int i = 0; i < a_qhgd.Length; i++)
            {
                string[] a_gtriQH = new string[9];
                a_gtriQH[0] = LibConvert.toString(((object[])a_qhgd[i])[0]);
                a_gtriQH[1] = b_so_id_ns;
                a_gtriQH[2] = LibConvert.toString(((object[])a_qhgd[i])[1]);
                a_gtriQH[3] = LibConvert.toString(((object[])a_qhgd[i])[2]);
                a_gtriQH[4] = LibConvert.toString(((object[])a_qhgd[i])[3]);
                a_gtriQH[5] = LibConvert.toString(((object[])a_qhgd[i])[4]);
                a_gtriQH[6] = LibConvert.toString(((object[])a_qhgd[i])[5]);
                a_gtriQH[7] = LibConvert.toString(((object[])a_qhgd[i])[6]);
                a_gtriQH[8] = LibConvert.toString(((object[])a_qhgd[i])[7]);

                LibTable.AddNewRow(ref b_dt_qh, a_gtriQH);
            }
            SqlData.ExeCuteProcedure(b_dt_qh, "PSINHVIEN_QHGD_NH");

            //string b_so_id_ns="201307200000002";
            // Quá trình đào tạo
            // DateTime t = DateTime.UtcNow;
            //System.TimeZoneInfo.ConvertTimeFromUtc(
            DataTable b_dt_dt = new DataTable();
            LibTable.AddNewCol(ref  b_dt_dt, new string[] { "ma_dt", "ma_cb", "loaidt", "tu_ngay", "den_ngay", "nganh_dt", "truong_dt", "xep_loai" });
            for (int i = 0; i < a_gtriGridDT.Length; i++)
            {
                string[] a_gtriQH = new string[b_dt_dt.Columns.Count];
                a_gtriQH[0] = LibConvert.toString(((object[])a_gtriGridDT[i])[0]);
                a_gtriQH[1] = b_so_id_ns;
                a_gtriQH[2] = LibConvert.toString(((object[])a_gtriGridDT[i])[2]);
                a_gtriQH[3] = LibConvert.DateToNumber(((object[])a_gtriGridDT[i])[3]);
                a_gtriQH[4] = LibConvert.DateToNumber(((object[])a_gtriGridDT[i])[4]);
                a_gtriQH[5] = LibConvert.toString(((object[])a_gtriGridDT[i])[5]);
                a_gtriQH[6] = LibConvert.toString(((object[])a_gtriGridDT[i])[6]);
                a_gtriQH[7] = LibConvert.toString(((object[])a_gtriGridDT[i])[7]);

                LibTable.AddNewRow(ref b_dt_dt, a_gtriQH);
            }
            SqlData.ExeCuteProcedure(b_dt_dt, "PQT_DAO_TAO_NH");
            //Quá trình công tác
            DataTable b_dt_ct = new DataTable();
            LibTable.AddNewCol(ref  b_dt_ct, new string[] { "qt_ct", "ma_cb", "ngaydct", "ngaycct", "vi_tri", "cong_ty", "chuc_vu" });
            for (int i = 0; i < a_gtriGridCT.Length; i++)
            {
                string[] a_gtriQH = new string[b_dt_ct.Columns.Count];
                a_gtriQH[0] = LibConvert.toString(((object[])a_gtriGridCT[i])[0]);
                a_gtriQH[1] = b_so_id_ns;
                a_gtriQH[2] = LibConvert.DateToNumber(((object[])a_gtriGridCT[i])[2]);
                a_gtriQH[3] = LibConvert.DateToNumber(((object[])a_gtriGridCT[i])[3]);
                a_gtriQH[4] = LibConvert.toString(((object[])a_gtriGridCT[i])[4]);
                a_gtriQH[5] = LibConvert.toString(((object[])a_gtriGridCT[i])[5]);
                a_gtriQH[6] = LibConvert.toString(((object[])a_gtriGridCT[i])[6]);

                LibTable.AddNewRow(ref b_dt_ct, a_gtriQH);
            }
            SqlData.ExeCuteProcedure(b_dt_ct, "PQT_CONGTAC_NH");
            //Quá trình khen thuong
            DataTable b_dt_kt = new DataTable();
            LibTable.AddNewCol(ref  b_dt_kt, new string[] { "id_kt", "ma_cb", "ngay_kt", "danh_hieu", "so_qd", "hinh_thuc", "dvi_kt" });
            for (int i = 0; i < a_gtriGridKT.Length; i++)
            {
                string[] a_gtriQH = new string[b_dt_kt.Columns.Count];
                a_gtriQH[0] = LibConvert.toString(((object[])a_gtriGridKT[i])[0]);
                a_gtriQH[1] = b_so_id_ns;
                a_gtriQH[2] = LibConvert.DateToNumber(((object[])a_gtriGridKT[i])[2]);
                a_gtriQH[3] = LibConvert.toString(((object[])a_gtriGridKT[i])[3]);
                a_gtriQH[4] = LibConvert.toString(((object[])a_gtriGridKT[i])[4]);
                a_gtriQH[5] = LibConvert.toString(((object[])a_gtriGridKT[i])[5]);
                a_gtriQH[6] = LibConvert.toString(((object[])a_gtriGridKT[i])[6]);

                LibTable.AddNewRow(ref b_dt_kt, a_gtriQH);
            }
            SqlData.ExeCuteProcedure(b_dt_kt, "PKHEN_THUONG_NH");
            //Ky luat
            DataTable b_dt_kl = new DataTable();
            LibTable.AddNewCol(ref  b_dt_kl, new string[] { "id_kl", "ma_cb", "ngay_kl", "ly_do", "so_qd", "hinh_thuc", "dvi_kl" });
            for (int i = 0; i < a_gtriGridKL.Length; i++)
            {
                string[] a_gtriQH = new string[b_dt_kl.Columns.Count];
                a_gtriQH[0] = LibConvert.toString(((object[])a_gtriGridKL[i])[0]);
                a_gtriQH[1] = b_so_id_ns;
                a_gtriQH[2] = LibConvert.DateToNumber(((object[])a_gtriGridKL[i])[2]);
                a_gtriQH[3] = LibConvert.toString(((object[])a_gtriGridKL[i])[3]);
                a_gtriQH[4] = LibConvert.toString(((object[])a_gtriGridKL[i])[4]);
                a_gtriQH[5] = LibConvert.toString(((object[])a_gtriGridKL[i])[5]);
                a_gtriQH[6] = LibConvert.toString(((object[])a_gtriGridKL[i])[6]);

                LibTable.AddNewRow(ref b_dt_kl, a_gtriQH);
            }
            SqlData.ExeCuteProcedure(b_dt_kl, "PKY_LUATNH");
            return b_so_id_ns;
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    [OperationContract]
    public string Fs_NHANSU_NH_EXCEL(DataTable a)
    {
        SqlData.ExeCuteProcedure(a, "PNHAN_SU_NH_EXCEL");
        return "";
    }
    [OperationContract]
    public string Fs_NHAN_SU_TIM(string b_ten, string phong, string cv)
    {
        try
        {
            DataTable b_dt = new DataTable();
            b_dt = SqlData.GetData(new object[] { b_ten, phong, cv }, "PNHAN_SU_TIM");
            if (b_dt.Rows.Count == 0) throw new Exception("loi:Không có dữ liệu:Loi");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    //Phần công gianngr daajh cho nhân sự
    [OperationContract]
    public string Fs_TT_CHUNG_PHAN_CONG()
    {
        try
        {
            DanhMuc dm = new DanhMuc();
            DataTable b_dt_namhoc = c_ht.Fdt_NAM_HOC();
            DataTable b_dt_bm = c_ht.Fdt_BOMON_LKE();
           string b_pb = dm.Fs_PHONG_BAN_LKE();
            return LibTable.FormTableToJson(b_dt_namhoc)+"@"+ b_pb+"@"+LibTable.FormTableToJson(b_dt_bm);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
}
